//
//  NetWorkManager.m
//  SmartTalk
//
//  Created by 陈勃 on 14-12-22.
//  Copyright (c) 2014年 gameabc. All rights reserved.
//

#import "NetWorkManager.h"
#import "AsyncSocket.h"
@interface NetWorkManager ()
{
    AsyncSocket *socket;
}
@end
@implementation NetWorkManager
SYNTHESIZE_SINGLETON_FOR_CLASS(NetWorkManager)
#pragma mark AsyncSocketDelegate
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"willDisconnectWithError");
    [self reconnect];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"connect %@ port %d success",host,port);
    [sock readDataWithTimeout:-1 tag:0];
}

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSLog(@"receive data %@",data);

}
-(void)reconnect
{
    NSError *error = nil;
   
    socket =  [[AsyncSocket alloc] initWithDelegate:self];
    [socket connectToHost:@"192.168.1.100" onPort:1888 error:&error];
    if (error) {
        NSLog(@"error %@",error);
    }
}

-(void)connect
{
    
    socket =  [[AsyncSocket alloc] initWithDelegate:self];
    NSError *error = nil;
    [socket connectToHost:@"192.168.1.100" onPort:1888 error:&error];
    if (error) {
        NSLog(@"error %@",error);
    }

}


-(void)sendContent:(NSString *)content
{
    [socket writeData:[content dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:101];
   // [socket readDataWithTimeout:10 tag:101];
}
@end
